package org.yookiee.support.toolkits;


import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * FileName MapToolkit
 * Package org.jeecg.common.util
 * Description using builder to construct map
 * author cpstory
 * create 2023-10-14 8:05
 * version V1.0
 */
public class MapToolkit {

    public static <K,V> MapBuilder<K,V> accept(Map<K,V> map){
        return new MapBuilder<K,V>(map);
    }


    public static <K,V> MapBuilder<K,V> builder(Class<K> keyClass,Class<V> valueClass){
        return new MapBuilder<K,V>();
    }

    public static class MapBuilder<K,V>{
        private Map<K,V> map;

        public MapBuilder() {
            this.map = Maps.newHashMap();
        }

        public MapBuilder(Map<K, V> map) {
            this.map = map;
        }

        public MapBuilder<K,V> of(K k, V v){
            map.put(k,v);
            return this;
        }

        public Map<K,V> build(){
            return map;
        }
    }

    public static Map<String,String> jsonObjectToMap(JSONObject jsonObject){
        Map<String,String> transMap = Maps.newHashMap();
        if(jsonObject != null && !jsonObject.isEmpty()){
            for (String key : jsonObject.keySet()) {
                transMap.put(key,jsonObject.getString(key));
            }
        }
        return transMap;
    }


    /*public static void main(String[] args) {
        // USE EXMAPLE
        Map<String, Integer> build = MapToolkit.builder(String.class,Integer.class)
                .of("a", 1)
                .of("b", 2)
                .build();
        System.out.println(build);

        Map<String, Integer> build1 = MapToolkit.accept(build)
                .of("c", 3)
                .of("d", 4)
                .build();
        System.out.println(build1);
    }*/

}
